#include"common_headers.h"

int deletedir31(char path[300])
{
	//DECLARATIONS
    int q, breaking_due_to_error, breaking_due_to_invalid_suppied_path, collect, state, state1, is_file_exist_in_nary, length_of_dir_name, is_mounted;
    char current_character, next_character;
    struct BST_Node *tmp;
        
    //INITIALIZATIONS
    q=0;
    state=0;
    state1=0;
    breaking_due_to_error=0;
    breaking_due_to_invalid_suppied_path=0;
    collect=0;
    length_of_dir_name=0;
    is_file_exist_in_nary=0;   
    is_mounted=0; 
    
    length_of_dir_name = strlen(path);   
    
    //VALIDATING AND OPERATING
    is_mounted = validateMountState();    
    if(is_mounted)
    {
    	//VALIDATE THE FORMAL ARGUMENTS RECEIVED FOR CORRECT FORMAT     	
    	for(q=0; q < length_of_dir_name; q++)
	   	{
	   		current_character = path[q];
	   		next_character = path[q+1];
	   		
	   		//CHECK IF THE DIRECTORY PATH RECEIVED CONTAINS TWO CONSECUTIVE SLASH '//' (NOT ALLOWED)
	   		if(current_character=='/' && next_character=='/')
	   		{
		   		//CONSECUTIVE '/' ARE NOT ALLOWED, HENCE BREAKING DUE TO ERROR
	   			breaking_due_to_error=1;
	   			break;
	   		}	   		
	   	}	
	   	
	   	//CHECK IF NOT THE INCORRECT INPUT DIRECTORY PATH
	   	if((breaking_due_to_error!=1))
	   	{
	   		//CHECK IN BST IF DIRECTORY EXISTS
	   		tmp=search(bst_root,path);
			if(1)
			{
				//CHECK IN N-ARY IF DIRECTORY EXISTS				
				is_file_exist_in_nary = search_nary(path);
				if(is_file_exist_in_nary)
				{
				   	//DELETE FROM N-ARY
				   	if(delete_nary(path))
				   	{
				   		//DELETE FROM OTHER DATA STRUCTURES (BST, HASH TABLE)
				   		deleteFromHashTable(ht_start, path);
		    			delete_bstnode(bst_root,path);
		    			collect=1;
		    			printf("deletedir_SUCCESS\n");
				   	}
				   	else
				   	{
				   		//DIRECTORY_IS_NOT_EMPTY (b'coz nary is not able to delete)
				   		printf("deletedir_FAILURE %s\n", ERR_VFS_DELETEDIR_02);
				   	}
				}
				else
				{
					//CANNOT_FIND_SPECIFIED_PATH_OR_DIR
				   	printf("deletedir_FAILURE %s\n", ERR_VFS_DELETEDIR_01);
				}
			}	
			else
			{
			 	//CANNOT_FIND_SPECIFIED_PATH_OR_DIR
			 	printf("deletedir_FAILURE %s\n", ERR_VFS_DELETEDIR_01);
			}	
	   	}
	   	else
	   	{
	   		//INVALID_DIRNAME
			printf("deletedir_FAILURE %s\n", ERR_VFS_DELETEDIR_03);
	   	}
    }
    else
    {
    	//VFS_NOT_MOUNTED
		printf("deletedir_FAILURE %s\n", ERR_VFS_DELETEDIR_04);
    }    
    return collect;
}
